﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SF.Common;

namespace SF.Ucenter.admin.contract
{
    public partial class contract_child_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");
            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.sf_contract_child().Exists(this.id))
                {
                    JscriptMsg("记录不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("contract_list", DTEnums.ActionEnum.View.ToString()); //检查权限
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    TypeBind(ddlPaytype);
                    ShowInfo(this.id);
                }

                if (action == DTEnums.ActionEnum.Add.ToString())//添加子合同,给合同编号赋值
                {
                    TypeBind(ddlPaytype);
                    string num = Request["contract_num"] == null ? "" : Request["contract_num"];
                    string busniessnum = Request["busnieseNum"] == null ? "" : Request["busnieseNum"];
                    BLL.sf_contract_child bll = new BLL.sf_contract_child();
                    int a = bll.GetRecordCount(num);
                    object appid = bll.GetAppid(busniessnum);
                    //设置添加子合同数量
                    if (a == 5)
                    {
                        JscriptMsg("合同数量已达标,无需再添加！", "contract_list.aspx");
                    }

                    //赋值合同号
                    txtcontractnum.Text = num.ToString();
                    //赋值公众号
                    if (appid != null)
                    {
                        txtappNum.Text = appid.ToString();
                    }
                    else 
                    {
                        JscriptMsg("该商户编号信息不存在！", "contract_list.aspx");
                    }
                }
            }
        }

        #region 收费类型=================================
        private void TypeBind(DropDownList ddl)
        {
            ddl.Items.Clear();
            ddl.Items.Add(new ListItem("请选择收费类型...", "-1"));
            ddl.Items.Add(new ListItem("按每笔收费", "0"));
            ddl.Items.Add(new ListItem("按超额收费", "1"));
        }
        #endregion

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.sf_contract_child bll = new BLL.sf_contract_child();
            Model.sf_contract_child model = bll.GetModel(_id);

            txtcontractnum.Text = model.contract_num;
            txtappNum.Text = model.appNum;
            txtcreatepeople.Text = model.create_people;
            ddlPaytype.SelectedValue = model.pay_type.ToString();
            txtprice_one.Text = model.price_one.ToString();
            txtprice_low.Text = model.price_low.ToString();
            txtprice_low_one.Text = model.price_low_one.ToString();
            txtprice_height.Text = model.price_height.ToString();
            txtprice_height_two.Text = model.price_height_two.ToString();
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            Model.sf_contract_child model = new Model.sf_contract_child();
            BLL.sf_contract_child bll = new BLL.sf_contract_child();

            model.contract_num = txtcontractnum.Text;
            model.appNum = txtappNum.Text;
            model.create_people = txtcreatepeople.Text;
            model.pay_type = int.Parse(ddlPaytype.SelectedValue);
            model.price_one = decimal.Parse(txtprice_one.Text.ToString());
            model.price_low = decimal.Parse(txtprice_low.Text.ToString());
            model.price_low_one = decimal.Parse(txtprice_low_one.Text.ToString());
            model.price_height = decimal.Parse(txtprice_height.Text.ToString());
            model.price_height_two = decimal.Parse(txtprice_height_two.Text.ToString());
            model.create_date = DateTime.Now;

            if (bll.Add(model) > 0)
            {
                AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加子合同:" + model.contract_num); //记录日志
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.sf_contract_child bll = new BLL.sf_contract_child();
            Model.sf_contract_child model = bll.GetModel(_id);

            model.contract_num = txtcontractnum.Text;
            model.appNum = txtappNum.Text;
            model.create_people = txtcreatepeople.Text;
            model.pay_type = int.Parse(ddlPaytype.SelectedValue);
            model.price_one = decimal.Parse(txtprice_one.Text.ToString());
            model.price_low = decimal.Parse(txtprice_low.Text.ToString());
            model.price_low_one = decimal.Parse(txtprice_low_one.Text.ToString());
            model.price_height = decimal.Parse(txtprice_height.Text.ToString());
            model.price_height_two = decimal.Parse(txtprice_height_two.Text.ToString());
            model.update_date = DateTime.Now;

            if (bll.Update(model))
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改商户子合同:" + model.contract_num); //记录日志
                result = true;
            }

            return result;
        }
        #endregion

        #region 提交操作=================================
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string num = txtcontractnum.Text;
            if (ddlPaytype.SelectedItem.Text == "请选择收费类型...")
            {
                Response.Write("<script>alert('请选择正确收费类型!!!')</script>");
            }
            else
            {
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ChkAdminLevel("contract_child_list", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                    if (!DoEdit(this.id))
                    {
                        if (this.id == 1)
                        {
                            JscriptMsg("不可禁用超级用户！", "contract_child_list.aspx");
                        }
                        else
                        {
                            JscriptMsg("保存过程中发生错误！", "");
                        }
                        return;
                    }
                    JscriptMsg("修改管理员信息成功！", "contract_child_list.aspx?contract_num=" + num);
                }
                else //添加
                {
                    ChkAdminLevel("contract_child_list", DTEnums.ActionEnum.Add.ToString()); //检查权限
                    if (!DoAdd())
                    {
                        JscriptMsg("保存过程中发生错误！", "");
                        return;
                    }
                    JscriptMsg("添加管理员信息成功！", "contract_child_list.aspx?contract_num=" + num);
                }
            }
        }
        #endregion
    }
}